package com.casems.controller;


import com.casems.commons.dto.PermissionDTO;
import com.casems.commons.vo.ResultVO;
import com.casems.entity.Permission;
import com.casems.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tch
 * @since 2023-05-19
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/getAllPermission")
    public ResultVO getAllPermission() {
        List<PermissionDTO> permissionList = permissionService.getAllPermission();
        List<PermissionDTO> top = new ArrayList<>();

        permissionList.forEach(permission -> {
            if(permission.getPid()==-1)
                top.add((PermissionDTO) permission);
        });
        for(PermissionDTO permission : top){
            permissionService.setChildren(permission,permissionList);
        }
        return top!=null&&top.size()!=0?ResultVO.success(top):ResultVO.failed("查询失败");
    }
}

